package com.huabo.utils;

import com.huabo.model.User;
import com.huabo.session.SessionModel;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashSet;
import java.util.List;

/**
 * Created by Jackie Liu on 2017/4/8.
 */
public class ApiUtils {

    private ApiUtils() {

    }

    /**
     * 获取登录用户信息
     * @param request
     * @return
     */
    public static User getLoginUser(HttpServletRequest request) {
        SessionModel sessionModel = (SessionModel) request.getAttribute(Constants.SESSION_MODEL);
        if (sessionModel != null) {
            return (User) sessionModel.getUser();
        }
        return null;
    }

    /**
     * 去除重复节点
     * @param list
     */
    public static void removeDuplicate(List<Long> list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
    }

    /**
     * 获取临时目录
     * @param request
     * @return
     */
    public static String getTempPath(HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        sb.append(request.getSession().getServletContext().getRealPath("/"));
        sb.append("/upload/temp/");
        File file = new File(sb.toString());
        //System.out.println("################## file path = " + file.getAbsolutePath());
        if (!file.exists()) {
            file.mkdirs();
        }
        return sb.toString();
    }
}
